/**
 * var resource = $('input').validation().add(function(){}, []);
 * $('input').validation().remove(resource);
 * $('input').validation().clear();
 */
(function($) {

$.fn.validation = function() {
    var options = $.fn.validation.defaults,
        elements = this,
        
        onSubmit = function()
        {
            $('[id*=validationWarning]').remove();
            var isValid = true;
            $(this).find('input,select,textarea').each(function(){
                var $element     = $(this);
                list         = $.data(this, options.index);
                isInputValid = true;

                if ($element.is(':disabled') || !list) {
                	return;
                }

                $.each(list, function(){
                    var procedure  = this[0],
                        parameters = this[1];
                    
                    try{
                        parameters = Array.prototype.slice.call(parameters);
                    }catch(e){
                        parameters = [];
                    }
                    parameters.unshift($element.val());
                    isInputValid = procedure.apply($element.get(0), parameters)
                                 ? isInputValid
                                 : false;
                });

                $element.toggleClass(options.classError, !isInputValid);
                isValid = isInputValid
                        ? isValid
                        : false;
                
                if (!isInputValid) {
                    //Removido por solicitação dos gestores
                    //$element.after('<img id="validationWarning-'+ $element.attr('name') +'" src="img/atencao.gif" style="vertical-align:middle;"/>');
                }
            });
            if (!isValid) {
                options.onError.apply($form);
                $(options.dialog).dialog('open'); 
            }
            return isValid;
        };

    $(options.dialog).dialog({
        bgiframe:  true,
        resizable: false,
        height:    140,
        modal:     true,
        autoOpen:  false,
        overlay: {
            backgroundColor: '#000',
            opacity: 0.5
        },
        buttons: {
            Voltar: function(){ $(this).dialog('close'); }
        }
    });
    
    return {
        add: function()
        {
            var rule = arguments,
            resource = null;
            elements.each(function()
            {
                var rules = $.data(this, options.index);
                var mask = null;

                if ((rule[0] == "notEmpty") || (rule[0] == "date") || (rule[0] == "emailAddress") || (rule[0] == "notIdentical")) {
                    if ((rule[0] ==  "notEmpty") && ($(this).attr('type') != 'hidden')) {
                        $(this).parent().addClass('asterisco');
                    }
                    
                    mask = eval('Rule.'+ rule[0]+'.mask');
                    rule[0] = eval('Rule.'+ rule[0]);
                    
                    rules = rules ? rules : [];
                    rules.push(rule);
                    $.data(this, options.index, rules);
                    
                    $form = $(this).parents('form');

                    $form.bind('submit.validation', onSubmit);
                    resource = rules.length - 1;
                }
            });
            return resource;
        },
        
        remove: function(){},
        
        clear: function()
        {
            var parameters = arguments;
            return elements.each(function()
            {
                $(this).parent().removeClass('asterisco');
                $.data(this, options.index, []);
            });
        }
   };
};

$.fn.validation.defaults = {
    index: 'a617908b172c473cb8e8cda059e55bf0',
    classError: 'error',
    onSuccess: function(){},
    onError: function(){},
    dialog: '#validation-dialog'
};

})(jQuery);

Rule = {};